home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14310 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.4 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why should I use classes....or should I?
  5. Date: Fri, 29 Mar 1996 13:31:08 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <315C2C6C.60FF@datalytics.com>
  8. References: <4jcohu$7c8@artemis.it.luc.edu>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Rick Olson wrote:
  16. > I am an experienced FORTRAN programmer and have a reasonable
  17. > knowledge of C.  Most of my programming is related to my research
  18. > which is generally related to constructing and comparing heuristic
  19. > alogorithms for difficult problems.  I am about to start a new
  20. > project and am trying to decide whether C++ is appropriate.  I'd
  21. > appreciate any feedback.
  22. > Suppose I want to compare 10 algorithms for solving a problem (eg
  23. > bin packing).  With C I would code the routines for each algorithm
  24. > and then write something to read the data and run it through each
  25. > algorithm printing the results.  From where I sit this is pretty
  26. > straight forward.  All the algorithms require the same input
  27. > data (perhaps item sizes) and reports the same results (number of
  28. > bins, solution time...).  This seems to be a function driven
  29. > application rather than a data driven app.
  30.  
  31. From what I understand of what you describe, the algorithms 
  32. would be normal functions as you would do in C.  However, the 
  33. data could be encapsulated nicely in a class.  Member functions 
  34. could simplify access by, for example, setting several related 
  35. values at once, validating relationships when you set values, 
  36. etc.  The advantage of that is that the functions implementing 
  37. the algorithms can reuse that logic by simply invoking the 
  38. member function on the data object it was given.
  39.  
  40. The other thing you mentioned was that they need to report the 
  41. results.  You can write a function to call from the algorithm 
  42. functions to do this, or you can write a member function to do 
  43. it.  That's a little fuzzier because the class may not know the 
  44. entire output required by the various algorithm functions.  On 
  45. the other hand, the class would know the data types intimately 
  46. and could produce output appropriate to various parts of the 
  47. data that the algorithm functions could call at appropriate 
  48. times.
  49.  
  50. The thing to remember about classes--before you even consider 
  51. advanced language features--is the encapsulation they provide.  
  52. This means that you have specified an interface to select pieces 
  53. of data and the functions that may directly interact with that 
  54. data.  You can do the same things with C, but C++ gives you a 
  55. language facility that protects you from your mistakes.
  56.  
  57. Once you declare your data private in a class, only member 
  58. functions may access it.  This ensures that you handle that data 
  59. reliably in all cases.  Any other functions that wish to use 
  60. that data must do so through the interface you prescribe in the 
  61. class declaration.  The compiler will complain if you try to 
  62. access data any way other than specified by the class 
  63. declaration.
  64.  
  65. > There are a few features of C++ that are more appealing than C.
  66. > Not needing to pass integer addresses to functions, then dereference
  67.  
  68. I assume you are referring to passing by reference.  Yes, that 
  69. is a nice addition.
  70.  
  71. > them is one.  Right now I don't see any compelling argument for
  72. > using classes unless it is to learn how they work.  In fact, it seems
  73. > to me that if I use classes I will actually pay a performance penalty
  74. > because of the overhead associated with binding functions that can't
  75. > be done at compilation time because of the possibility of polymorphism.
  76.  
  77. You only incur this overhead when you use virtual functions and 
  78. when calling such functions through a pointer or reference to a 
  79. class.  If you use an object directly, the compiler invokes the 
  80. member function of the object's class directly, avoiding the 
  81. virtual function mechanism.
  82.  
  83. > Am I missing something?  Is there a reason why I should be trying to
  84. > think in terms of objects rather than thinking of C++ as "a better C".
  85.  
  86. That's a decent reason in and of itself.
  87.  
  88. > I would appreciate any personal opinions/experience and pointers to
  89. > good books that may help me understand the *WHY* of OOP for these
  90. > types programs rather than just the *HOW* of classes.
  91. > Thanks in advance--
  92. > Rick Olson
  93.  
  94. -- 
  95. Robert Stewart        | My opinions are usually my own.
  96. Datalytics, Inc.    | stew@datalytics.com
  97.